use serde::{Deserialize, Serialize};
use wither::{
    bson::{oid::ObjectId, DateTime},
    Model,
};
/// ## 喜欢列表的内容
/// 有多种喜欢类型，每种喜欢类型可能会有不同的缓存数据
/// ## TODO:
/// 1. source_id每种类型都有，因为设计上是用户喜欢了某个其他事物
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum LikeListContent {
    #[serde(rename = "club")]
    Club {
        source_id: ObjectId,
        target_id: ObjectId,
    },
    #[serde(rename = "user")]
    User {
        source_id: ObjectId,
        target_id: ObjectId,
    },
    #[serde(rename = "article")]
    Article {
        source_id: ObjectId,
        target_id: ObjectId,
    },
}
#[derive(Debug, Clone, Serialize, Deserialize, Model)]
pub struct LikeListModel {
    #[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
    pub id: Option<ObjectId>,
    pub content: LikeListContent,
    pub created_at: DateTime,
}
impl LikeListModel {
    pub fn new<C: Into<LikeListContent>>(content: C) -> Self {
        Self {
            id: None,
            content: content.into(),
            created_at: DateTime::now(),
        }
    }
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LikeListModelDTO {
    pub id: String,
    pub content: LikeListContent,
    pub created_at: chrono::DateTime<chrono::Utc>,
}
impl From<LikeListModel> for LikeListModelDTO {
    fn from(m: LikeListModel) -> Self {
        Self {
            id: m.id.as_ref().map(ToString::to_string).unwrap_or_default(),
            content: m.content,
            created_at: m.created_at.to_chrono(),
        }
    }
}
